import React, {useRef} from 'react';
import {useNavigate} from "react-router-dom";

const userList =  ['zhangsan','lisi','wangwu']
function Login(props) {
    const userRef = useRef()
    const navigate = useNavigate()
    const login = (e)=>{
        if(e.keyCode === 13){
            if(userList.includes(userRef.current.value)){
                localStorage.setItem('token',userRef.current.value)
                navigate(-1)//编程式导航
            }else{
                alert('用户不存在！')
            }

        }
    }
    return (
        <div>
            <h3>登录</h3>
            <input onKeyUp={login} ref={userRef} type='text' placeholder='请输入用户名'/>
        </div>
    );
}

export default Login;